home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!bounce-back
- Date: 10 Jan 96 23:17:42 GMT
- Approved: fjh@cs.mu.oz.au
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Newsgroups: comp.std.c++
- Subject: Re: Q: f( const T )
- X-Original-Date: 10 Jan 1996 16:22:57 GMT
- Organization: Sun Microsystems Inc.
- Message-ID: <4d0p51$il8@engnews1.Eng.Sun.COM>
- References: <4cvsps$5ig@dub-news-svc-4.compuserve.com>
- Reply-To: clamage@Eng.Sun.COM
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMPRJL+EDnX0m9pzZAQHpIQGAn9dI8Fq+PqOHJlBzuKJdd4R3qDxjuxaU
- txrVZ3VeMBdr+lMg73I51/xgz20tmMWT
- =gC1f
-
- In article 5ig@dub-news-svc-4.compuserve.com,
- 100754.2730@compuserve.com (Martin Aupperle) writes:
- >If I have a function declared as
- >
- > f( const T );
- >
- >will a local T be created and initialized with the copy constructor?
- >I hope so, but people pointed out to me that f cannot change anything
- >in the object and therefore can work on the caller's object too, and
- >therefore a compiler may optimize away the local object.
- >What about mutable members? Since we now can have these, it makes a
- >difference. Who is right?
-
- The semantics implemented by the compiler must be those of pass by value,
- whatever mechanism is actually used. In particular, if the program
- contains only code with defined behavior, the actual argument must
- not be changed by the call to f() -- whether the formal argument is
- declared const or not.
-
- In particular, if type T has a mutable member, the compiler cannot pass
- the actual argument by reference unless it can prove that the argument
- is in fact never altered.
-
- If the formal argument is declared const and has no mutable members,
- casting away const and attempting to modify the parameter has undefined
- results.
-
- The fundamental rule is the "as-if" rule. The language definition
- describes the effects of various constructs. The standard describes an
- abstract machine for purposes of explanation of the required effects.
- An implementation that behaves (so far as a valid program can detect)
- as if it were the abstract machine is a valid implementation.
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
- Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
- is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
-